package core

import (
	"bytes"
	"fmt"
	"github.com/Sir-yuChen/funAnalyse/global"
	"github.com/Sir-yuChen/funAnalyse/initialize"
	"github.com/Sir-yuChen/funAnalyse/initialize/mongodb"
	"github.com/Sir-yuChen/funAnalyse/initialize/mqConsumer"
	"github.com/Sir-yuChen/funAnalyse/initialize/validator"
	mongodb2 "github.com/Sir-yuChen/funAnalyse/utils/mongodb"
	"go.uber.org/zap"
	"os"
	"os/exec"
)

type server interface {
	ListenAndServe() error
}

func RunWindowsServer() {
	//更新接口文档
	cmd := exec.Command("swag", "init")
	var out bytes.Buffer
	cmd.Stdout = &out
	cmd.Stderr = os.Stderr
	err := cmd.Start()
	if err != nil {
		global.GVA_LOG.Error("接口文档更新异常")
	} else {
		global.GVA_LOG.Info("接口文档已更新》》》》》》》")
	}
	if global.GVA_CONFIG.System.UseMultipoint || global.GVA_CONFIG.System.UseRedis {
		// 初始化redis服务
		err := initialize.Redis()
		if err != nil {
			global.GVA_LOG.Error("redis初始化异常！！！")
			return
		}
	}
	// mongodb 初始化
	if err = mongodb.InitMongoDB(); err != nil {
		global.GVA_LOG.Error("MongoDb初始化异常！！！", zap.Error(err))
		return
	}
	mongodb2.InitMongoDBCollection()
	//初始化校验翻译器
	validator.InitTrans("zh")
	//路由配置
	Router := initialize.Routers()
	address := fmt.Sprintf(":%d", global.GVA_CONFIG.System.Addr)
	global.GVA_LOG.Info("监听端口", zap.String("prot", address))
	s := initServer(address, Router)
	//启动系统调用mq队列的监听 注意：只有初始化完成之后，才可以调起mq监听队列
	go mqConsumer.InitListenMq()
	// 保证文本顺序输出
	e := s.ListenAndServe().Error()
	if e != "" {
		global.GVA_LOG.Error(e)
	}

}
